// pages/teacherRegistPage/teacherRegistPage.js
var Mcaptcha = require('../../utils/mcaptcha.js');
var app=getApp();
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    teacherId: undefined,
    teacherEmail: undefined,
    password: undefined,
    ensurepassword: undefined,
    passwordFlag: undefined,
    ensureFlag: undefined,
    idFlag: undefined,
    emailFlag: undefined,
    bindCode: undefined,
    name: undefined,
    nameFlag:undefined,
    codeFlag: -2
  },

  getBindCode: function (e) {
    this.setData({
      bindCode: e.detail.value
    })
    this.checkBindCode();
  },
  checkBindCode: function () {
    var that = this
    var iscode = this.mcaptcha.validate(this.data.bindCode);
    if (this.data.bindCode == undefined) {
      that.setData({
        codeFlag: 0
      })
    } else if (iscode) {
      that.setData({
        codeFlag: 1
      })
    } else {
      that.setData({
        codeFlag: -1
      })
    }
    console.log(this.data.codeFlag);
  },
  changeBind() {
    this.mcaptcha.refresh();
  },


  goLogin: function () {
    wx.redirectTo({
      url: '/pages/teacherLogPage/teacherLogPage',
    })
  },
  
  isValidEmail: function (str) {
    var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if (reg.test(str)) {
      return true;
    }
    return false;
  },

  isValidName: function (str) {
    var reg = /^[\u4e00-\u9fa5 ]{2,4}$/;
    if (reg.test(str)) {
      return true;
    }
    return false;
  },

  getName: function (e) {
    if (e.detail.value) {
      this.setData({
        name: e.detail.value
      })
    }
    var that = this;
    var currentFlag = 1;
    var isValid = this.isValidName(this.data.name);
    if (isValid) {
      that.setData({
        nameFlag: currentFlag
      })
    }
    else {
      wx.showToast({
        title: '请输入正确姓名',
        icon: "none",
      })
    }
  },

  getTeacherEmail: function (e) {
    if (e.detail.value) {
      this.setData({
        teacherEmail: e.detail.value
      })
    }
    var that = this;
    var currentFlag = 1;
    var isValid = this.isValidEmail(this.data.teacherEmail);
    if (isValid) {
      that.setData({
        emailFlag: currentFlag
      })
    }
    else {
      wx.showToast({
        title: '请输入正确邮箱',
        icon: "none",
      })
    }
  },

  isValidPassword: function (str) {
    if ((str.length < 6) || (str.length > 20)) {
      return false;
    }

    if (/[^a-zA-Z0-9_]/.test(str)) {

      return false;

    }

    if (/(^[a-z]+$|^[A-Z]+$|^\d+$|^_+$)/.test(str)) {

      return false;

    }

    return true;

  },

  getPassword: function (e) {
    if (e.detail.value) {
      this.setData({
        password: e.detail.value
      })
    }
    var isLegal = this.isValidPassword(this.data.password);
    var that = this;
    if (isLegal) {
      that.setData({
        passwordFlag: 1
      })
    }
    else {
      wx.showToast({
        title: '密码应在6-20位间，包含下划线、字母、数字中的至少两种',
        icon: "none",
      })
    }
  },
  getTeacherId: function (e) {
    if (e.detail.value) {
      this.setData({
        teacherId: e.detail.value
      })
      var currentFlag = 0;
      var isLegal = 0;
      var that = this;
      var reg = /^[0-9]{10}$/g;
      if (!reg.test(this.data.teacherId)) {
        wx.showToast({
          title: '请输入合法工号,10位数字',
          icon: "none",
        })
      }
      else {
        isLegal = 1;
      }

      if (isLegal) {
        wx.request({//检测学号是否已存在 
          url: 'https://weixin0304.cn/isTeacherIdExist.php',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          data: {
            teacherId: this.data.teacherId,
          },

          success: function (res) {
            console.log(res);
            if (res.data == 0) {//这里待改
              //学号合法
              currentFlag = 1;
              that.setData({
                idFlag: currentFlag
              })
            } else {
              //用户名非法（学号在数据库已存在）
              currentFlag = 0;
              wx.showToast({
                title: '该工号已被注册',
                icon: "none",
              })
              that.setData({
                idFlag: currentFlag
              })
            }
          }
        })
      }
    }

  },

  getEnsurePassword: function (e) {
    if (e.detail.value) {
      this.setData({
        ensurepassword: e.detail.value
      })
      var currentFlag = 1
      var that = this;
      if (this.data.ensurepassword != this.data.password) {
        wx.showToast({
          title: '两次密码不一致',
          icon: "none"
        })
        currentFlag = 0
        that.setData({
          ensureFlag: currentFlag
        })
      } else {
        if (this.data.password) {
          that.setData({
            ensureFlag: currentFlag
          })
        }
      }
    }
  },
  getSecretKey: function(e){
    if (e.detail.value) {
      this.setData({
        secretKey: e.detail.value
    })
    }
},
  regist: function () {
    if ((undefined != this.data.password) && (undefined != this.data.teacherEmail) && (undefined != this.data.teacherId) && (undefined != this.data.ensurepassword)
      && (undefined != this.data.secretKey)&&this.data.bindCode&&this.data.name) {
     
      if(this.data.codeFlag!=1){
        wx.showToast({
          title: '请输入正确验证码',
          icon: "none"
        })
        this.changeBind();
      }
      else if (!this.data.idFlag) {
        wx.showToast({
          title: '请输入合法学号',
          icon: "none"
        })
      } 
      else if (!this.data.nameFlag) {
        wx.showToast({
          title: '请输入正确姓名',
          icon: "none"
        })
      }else if (!this.data.passwordFlag) {
        wx.showToast({/////eeeeeeee
          title: '两次密码不一致',
          icon: "none"
        })
      }
      else if (!this.data.emailFlag) {
        wx.showToast({
          title: '请输入正确邮箱',
          icon: "none"
        })
      }
      else {
        var that=this
        wx.request({
          url: 'https://weixin0304.cn/teacherRegist.php',
          data: {
            teacherId: this.data.teacherId,
            password: this.data.password,
            teacherEmail: this.data.teacherEmail,
            name:this.data.name,
            secretKey:this.data.secretKey
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: function (res) {
            console.log(res);
            if (res.data == 1) {
              wx.setStorageSync('id', that.data.teacherId);
              wx.setStorageSync('name', that.data.name);
              wx.setStorageSync('role', "老师");
              wx.showToast({
                title: '注册成功',
                icon: "none"
              })
              wx.redirectTo({
                url: "/pages/teacherAcPage/teacherAcPage?id=" + that.data.teachertId + "&name=" + that.data.name,
              })
              wx.set
            }
            else if(res.data==-1){
              wx.showToast({
                title: '密钥错误！',
                icon: "none",
              })
            }
            else{
              wx.showToast({
                title: '注册失败 未知错误',
                icon: "none",
              })
            }

          }

        })
      }
    } else {
      wx.showModal({
        title: '提示',
        content: '请将信息填写完整后提交',
        showCancel: false
      })
    }
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.mcaptcha = new Mcaptcha({
      el: 'canvas',
      width: 80,
      height: 35,
      createCodeImg: ""
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})